// pages/buyVip/buyVip.js
var http = require('../../../utils/http.js');
var config = require('../../../utils/config.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    duration: 1000,
    userInfo: {},
    userLevelInfo: {}, // 当前会员信息
    premiumVipList: [], // 付费会员等级列表
    currentLevelId: 0,
    selectPremiumVip: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 获取会员信息
    this.getUserLevelInfo()
    // 获取年费会员列表
    this.getPayMemberList()
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.setData({

    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 获取当前会员信息
   */
  getUserLevelInfo() {
    var params = {
      url: '/p/score/scoreLevel/page',
      method: 'GET',
      data: {
        levelType: 0
      },
      callBack: res => {
        this.setData({
          userLevelInfo: res,
          currentLevelId: res.userLevel.id,
          currentGrowth: res.growth
        })
      }
    }
    http.request(params)
  },

  /**
   * 获取年费会员列表
   */
  getPayMemberList() {
    var params = {
      url: '/p/score/scoreLevel/page',
      method: 'GET',
      data: {
        levelType: 1
      },
      callBack: res => {
        this.setData({
          premiumVipList: res.userLevels,
          selectPremiumVip: res.userLevels[0],
        })
      }
    }
    http.request(params)
  },

  /**
   * 立即购买- 根据当前付费会员id
   */
  buyNow() {
    if (this.data.currentGrowth < this.data.selectPremiumVip.needGrowth){
      return wx.showToast({
        title: '成长值不足，无法购买',
        icon: 'none'
      })
    }

    if (this.data.userLevelInfo.levelType != 0 && this.data.userLevelInfo.userLevel.level < this.data.selectPremiumVip.level) {
      // 已有付费会员时购买付费会员
      wx.showModal({
        title: '提示',
        content: `您当前为${this.data.userLevelInfo.userLevel.levelName}，即将购买${this.data.selectPremiumVip.levelName}，升级会员将覆盖当前会员时限，是否确认购买？`,
        success: res => {
          if (res.confirm) {
            this.buyVip()
          } else {

          }
        }
      })
      return
    }
    this.buyVip()
  },


  /**
   * 购买会员
   */
  buyVip(){
    var ths = this
    var params = {
      url: '/p/level/payLevel',
      method: 'POST',
      data: {
        id: this.data.selectPremiumVip.id,
        scene: '',
        payeType: 1
      },
      callBack: res => {
        // 微信支付返回参数
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.packageValue,
          signType: res.signType,
          paySign: res.paySign,
          appId: res.appId,
          success: e => {
            console.log("支付成功");
            wx.showModal({
              title: '提示',
              content: '购买成功',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  ths.getUserLevelInfo()
                }
              }
            })
          },
          fail: err => {
            console.log("支付失败");
            wx.showModal({
              title: '提示',
              content: '支付失败，请重新支付',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  ths.getUserLevelInfo()
                }
              }
            })
          }
        })
      }
    }
    http.request(params)
  },

  /**
   * 切换轮播图
   */
  selectVip(e) {
    this.setData({
      selectPremiumVip: this.data.premiumVipList[e.detail.current]
    })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
